package com.liao.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liao.seckill.exception.GlobalException;
import com.liao.seckill.mapper.UserMapper;
import com.liao.seckill.pojo.User;
import com.liao.seckill.service.UserService;
import com.liao.seckill.util.CookieUtil;
import com.liao.seckill.util.MD5Util;
import com.liao.seckill.util.UUIDUtil;
import com.liao.seckill.util.ValidatorUtil;
import com.liao.seckill.vo.LoginVo;
import com.liao.seckill.vo.RespBean;
import com.liao.seckill.vo.RespBeanEnum;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lzp
 * @version 1.0
 * lzpnb!
 */
@Service
public class UserServiceImpl  extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {

        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

//        if(!StringUtils.hasText(mobile) || !StringUtils.hasText(password) ){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }

//        if(!ValidatorUtil.isMobile(mobile)){
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }

        User user = userMapper.selectById(mobile);
        if(user == null){
            //return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw  new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        if(!MD5Util.midPassToDBPass(password,user.getSlat()).equals(user.getPassword())){
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }

        String ticket = UUIDUtil.uuid();
        //request.getSession().setAttribute(ticket,user);
        System.out.println(redisTemplate.hashCode());
        redisTemplate.opsForValue().set("user:" + ticket,user);
        CookieUtil.setCookie(request,response,"userTicket",ticket);

        return RespBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if(!StringUtils.hasText(userTicket)) {
            return null;
        }

        User user = (User)redisTemplate.opsForValue().get("user:" + userTicket);

        if(user != null) {
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }

        return user;
    }

    @Override
    public RespBean updatePassword(String userTicket, String password, HttpServletRequest request, HttpServletResponse response) {
        User user = getUserByCookie(userTicket, request, response);

        if(user == null) {
            throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST);
        }

        user.setPassword(MD5Util.inputPassToDBPass(password,user.getSlat()));
        int i = userMapper.updateById(user);
        if(i == 1){
            redisTemplate.delete("user:" + userTicket);
            return RespBean.success();
        }


        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
    }
}
